package com.vnp.cooking;

import org.json.JSONObject;

import com.google.android.gcm.GCMRegistrar;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.Common;
import com.vnp.cooking.utils.PreferenceHelper;

import android.R.integer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.ProgressBar;

public class SplashScreenActivity extends Activity {
	
	PreferenceHelper preferenceHelper;
	ProgressBar progressBar;
	Handler mHandler = new Handler();
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			initValue();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		preferenceHelper = new PreferenceHelper(this);
		
//		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		init();
		
		new GetAllNameAsynTask().execute("");
		
		registerDevice();
		
		
		mHandler.postDelayed(r, 1000);
		
	}
	
	
	void init() {
		
	}
	

	void initValue() {
		if (preferenceHelper.getID().equals("")) {
			// mo trang login
			openLogin();
		} else {
			// tu dong login
			gotoMain();
		}
	}
	
	void gotoMain() {
		Intent t = new Intent(SplashScreenActivity.this, MainActivity.class);
		finish();
		t.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(t);
	}
	
	void login() {
		Intent t = new Intent(SplashScreenActivity.this, LoginIDVatgiaActivity.class);
		finish();
		startActivity(t);
	}
	
	
	void openLogin() {
		Intent t = new Intent(SplashScreenActivity.this, LoginActivity.class);
		finish();
		startActivity(t);
	}
	
	void registerGCM() {
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		
		String redId = GCMRegistrar.getRegistrationId(this);
		
		// Check if regid already presents
		if (redId.equals("")) {
			GCMRegistrar.register(this, Common.PROJECT_NUMBER);
		}
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				String regId="";
				
				while (regId.equals("")) {
					regId = GCMRegistrar.getRegistrationId(SplashScreenActivity.this);
					Log.i("Tuan", "Registered with id "+regId);
//					PreferenceHelper.setJSDeviceId(SplashScreenActivity.this, regId);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
	public class GetAllNameAsynTask extends AsyncTask<String, Void, Void>{

		@Override
		protected Void doInBackground(String... params) {
			getAllName();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
		}
		
		
		
		
		
	}
	
	
	void getAllName(){
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
			}

			@Override
			public void onSuccess(JSONObject re) {
				super.onSuccess(re);
				preferenceHelper.setDataName(re.toString());
			}
			
		});
		
		httpClientHelper.getAllNameCT();
	}
	
	// dang ki device_id
	void registerDevice() {
		if (Build.FINGERPRINT.startsWith("generic")) {
		 // running on an emulator
			Log.e("EMULATOR", "======================");
		} else {
		// running on a device
		// Make sure the device has the proper dependencies.
			GCMRegistrar.checkDevice(SplashScreenActivity.this);
			
		// Make sure the manifest was properly set - comment out this line
		// while developing the app, then uncomment it when it's ready.	
			GCMRegistrar.checkManifest(SplashScreenActivity.this);
			
			String regId = GCMRegistrar.getRegistrationId(SplashScreenActivity.this);
			
			// Check if regid already presents
			if (regId.equals("")) {
				GCMRegistrar.register(this, Common.PROJECT_NUMBER);
			}
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					String regId="";
					
					while (regId.equals("")) {
						regId = GCMRegistrar.getRegistrationId(SplashScreenActivity.this);
						Log.i("Tuan", "Registered with id "+regId);
						preferenceHelper.setAnuongDeviceId(regId);
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}).start();
		}
	}


}
